﻿using System;

using SharedGenomics.Core;

namespace SharedGenomics.Workbench.Core
{
    public class CreationPresenterInitialisationStage : PresenterInitialisationStage
    {
        public override bool Initialise(IPresenterInitialisationContext context)
        {
            context.ThrowIfNull("context");

            if (context.Current == null)
            {
                Type presenterType = context.PresenterType;
                context.Container.ThrowIfNull<ArgumentException>("Cannot create presenter - container not set on context");
                ExceptionHelper.ThrowIf<ArgumentException>(!typeof(IPresenter).IsAssignableFrom(presenterType), "Cannot create presenter - type is not a presenter type");

                context.Current = (IPresenter)context.Container.Resolve(presenterType);
            }

            return true;
        }
    }
}
